package com.zh.business.service.taxi.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.zhbootcommon.entity.taxi.DTaxiAddressInfo;
import com.zh.zhbootcommon.entity.taxi.DTaxiOrderInfo;
import com.zh.business.mapper.taxi.DTaxiAddressInfoMapper;
import com.zh.business.mapper.taxi.DTaxiOrderInfoMapper;
import com.zh.business.mapper.taxi.DUserOrderRelationMapper;
import com.zh.business.service.taxi.IDTaxiOrderInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 打车订单表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-04
 */
@Service
public class DTaxiOrderInfoServiceImpl extends ServiceImpl<DTaxiOrderInfoMapper, DTaxiOrderInfo> implements IDTaxiOrderInfoService {

    @Autowired
    DTaxiOrderInfoMapper orderInfoMapper;

    @Autowired
    DTaxiAddressInfoMapper addressInfoMapper;

    @Autowired
    DUserOrderRelationMapper userOrderRelationMapper;

    @Override
    public Page<DTaxiOrderInfo> queryTaxiOrderPage(Integer page, Integer limit, String orderCode) {
        Page<DTaxiOrderInfo> orderInfoPage = new Page<>(page, limit);
        LambdaQueryWrapper<DTaxiOrderInfo> queryWrapper = new LambdaQueryWrapper<DTaxiOrderInfo>();
        if (StrUtil.isNotBlank(orderCode)) {
            queryWrapper.like(DTaxiOrderInfo::getOrderCode, orderCode);
        }
        queryWrapper.eq(DTaxiOrderInfo::getDelFlg, '0');
        orderInfoPage = orderInfoMapper.selectPage(orderInfoPage, queryWrapper);
        orderInfoPage.setTotal(orderInfoPage.getRecords().size());
        // 渲染地址信息
        Optional.ofNullable(orderInfoPage.getRecords()).orElse(new ArrayList<>())
                .forEach(a -> {
                    if (a.getTaxiId() > 0) {
                        DTaxiAddressInfo addressInfo = addressInfoMapper.selectById(a.getTaxiId());
                        if (Objects.nonNull(addressInfo)) {
                            a.setTaxiAddress(addressInfo.getTaxiFirst() + "-" + addressInfo.getTaxiLast());
                        }
                    }
                });
        return orderInfoPage;
    }

    @Override
    public DTaxiOrderInfo queryTaxiOrderOne(Integer orderId) {
        DTaxiOrderInfo orderInfo = new DTaxiOrderInfo();
        if (orderId > 0){
            orderInfo = orderInfoMapper.selectById(orderId);
        }
        return orderInfo;
    }

    @Override
    public boolean removeTaxiOrder(Integer orderId) {
        boolean flag = false;
        DTaxiOrderInfo orderInfo = orderInfoMapper.selectById(orderId);
        if (Objects.nonNull(orderInfo)) {
            orderInfo.setDelFlg(1);
            Integer result = orderInfoMapper.updateById(orderInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }
}
